package com.jz.community.moduleshopping.orderDetail.adapter;

import android.support.annotation.Nullable;
import android.widget.ImageView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.jz.community.basecomm.constant.AppConstants;
import com.jz.community.basecomm.utils.BaseImageLoaderUtils;
import com.jz.community.basecomm.utils.CharacterUtils;
import com.jz.community.basecomm.utils.ConverterUtils;
import com.jz.community.basecomm.utils.Preconditions;
import com.jz.community.basecomm.utils.RxTextTool;
import com.jz.community.basecomm.utils.SHelper;
import com.jz.community.moduleshopping.R;
import com.jz.community.moduleshopping.orderDetail.bean.OrderDetailBean;

import java.util.List;

public class OrderDetailAdapter extends BaseQuickAdapter<OrderDetailBean.OrderItemListBean, BaseViewHolder> {

    public OrderDetailAdapter(int layoutResId, @Nullable List<OrderDetailBean.OrderItemListBean> data) {
        super(layoutResId, data);
    }

    /**
     * 订单商品基本信息
     *
     * @param helper
     */
    private void handleGoodsData(BaseViewHolder helper, OrderDetailBean.OrderItemListBean item) {
        ImageView imageView = helper.getView(R.id.iv_shop_goods_photo);
        BaseImageLoaderUtils.getInstance().loadDefaltImage(mContext, imageView, item.getGoodsImg());
        helper.setText(R.id.tv_shop_goods_name, item.getGoodsName());
        showGoodsPrice(helper, item.getDiscountPrice(), item.getPrice());
        helper.setText(R.id.tv_shop_goods_buy_count, "x" + item.getGoodsCount());
        helper.setText(R.id.tv_shop_goods_description, item.getSkuName());
    }

    /**
     * 显示商品价格
     *
     * @param discountPrice
     */
    private void showGoodsPrice(BaseViewHolder helper, String discountPrice, String price) {
        if (!Preconditions.isNullOrEmpty(discountPrice)) {
            helper.setText(R.id.tv_shop_goods_price, AppConstants.MONEY + discountPrice);
            SHelper.vis(helper.getView(R.id.tv_shop_goods_disPrice_tv));
            RxTextTool.getBuilder("").append(AppConstants.MONEY + CharacterUtils.roundByGoodPrice(ConverterUtils.toDouble(price))).setStrikethrough().into(helper.getView(R.id.tv_shop_goods_disPrice_tv));
        } else {
            helper.setText(R.id.tv_shop_goods_price, AppConstants.MONEY + price);
            SHelper.gone(helper.getView(R.id.tv_shop_goods_disPrice_tv));
        }
    }

    @Override
    protected void convert(BaseViewHolder helper, OrderDetailBean.OrderItemListBean item) {
        handleGoodsData(helper, item);
    }
}
